package com.zw.admin.server.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zw.admin.server.page.table.PageTableRequest;
import com.zw.admin.server.page.table.PageTableHandler;
import com.zw.admin.server.page.table.PageTableResponse;
import com.zw.admin.server.page.table.PageTableHandler.CountHandler;
import com.zw.admin.server.page.table.PageTableHandler.ListHandler;
import com.zw.admin.server.dao.PGoodsTypeDao;
import com.zw.admin.server.model.PGoodsType;
import com.zw.admin.server.model.Permission;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/pGoodsTypes")
public class PGoodsTypeController {

    @Autowired
    private PGoodsTypeDao pGoodsTypeDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public PGoodsType save(@RequestBody PGoodsType pGoodsType) {
        pGoodsTypeDao.insert(pGoodsType);

        return pGoodsType;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public PGoodsType get(@PathVariable Long id) {
        return pGoodsTypeDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public PGoodsType update(@RequestBody PGoodsType pGoodsType) {
        pGoodsTypeDao.update(pGoodsType);

        return pGoodsType;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public List<PGoodsType> list(PageTableRequest request) {
    	List<PGoodsType> pGoodsTypeAll = pGoodsTypeDao.listAll();
    	List<PGoodsType> list = new ArrayList<>();
    	setTypeList(0L,pGoodsTypeAll,list);
    	return list;
    }
    
    /**
	 * 菜单列表
	 * 
	 * @param pId
	 * @param pGoodsTypeAll
	 * @param list
	 */
	private void setTypeList(Long pId, List<PGoodsType> pGoodsTypeAll, List<PGoodsType> list) {
		for (PGoodsType pGoodsType : pGoodsTypeAll) {
			if (pGoodsType.getPid().equals(pId)) {
				list.add(pGoodsType);
				if (pGoodsTypeAll.stream().filter(p -> p.getPid().equals(pGoodsType.getId())).findAny() != null) {
					setTypeList(pGoodsType.getId(), pGoodsTypeAll, list);
				}
			}
		}
	}
	

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        pGoodsTypeDao.delete(id);
    }
    
    @GetMapping("/parents")
	@ApiOperation(value = "一级菜单")
	@RequiresPermissions("sys:menu:query")
	public List<PGoodsType> parentGoodsType() {
		List<PGoodsType> parents = pGoodsTypeDao.listParents();

		return parents;
	}
}
